﻿namespace DeltaGroup.WheelOfJeopardy.UI.Controls
{
    using System;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Controls.Primitives;
    using System.Windows.Media.Animation;

    using DeltaGroup.WheelOfJeopardy.UI.Events;

    /// <summary>
    /// Interaction logic for PowerMeter.xaml
    /// </summary>
    public sealed partial class PowerMeter : UserControl
    {
        #region Constructors

        /// <summary>
        /// Initializes a new instance of the <see cref="PowerMeter"/> class.
        /// </summary>
        public PowerMeter()
        {
            InitializeComponent();
        }

        #endregion Constructors

        #region Delegates

        /// <summary>
        /// Delegate for event handler
        /// </summary>
        public delegate void PowerLevelEventHandler(object sender, PowerLevelEventArgs e);

        #endregion Delegates

        #region Events

        /// <summary>
        /// Occurs when [power level captured].
        /// </summary>
        public event PowerLevelEventHandler PowerLevelCaptured;

        #endregion Events

        #region Properties

        /// <summary>
        /// Gets or sets the power level.
        /// </summary>
        /// <value>The power level.</value>
        public double PowerLevel
        {
            get { return powerBar.Value; }
            set { powerBar.Value = value; }
        }

        #endregion Properties

        #region Methods

        /// <summary>
        /// Resets the meter.
        /// </summary>
        public void ResetMeter()
        {
            GetPowerMeterStoryboard().ClearValue(RangeBase.ValueProperty);
            powerBar.BeginAnimation(RangeBase.ValueProperty, null);

            PowerLevel = 0;
            startPowerButton.IsEnabled = true;
        }

        /// <summary>
        /// Gets the power meter storyboard.
        /// </summary>
        /// <returns></returns>
        private Storyboard GetPowerMeterStoryboard()
        {
            Storyboard storyboard = this.Resources["powerMeterStoryboard"] as Storyboard;
            if (storyboard == null)
            {
                throw new MissingFieldException("Storyboard [powerMeterStoryboard] not found in PowerMeter resources.");
            }

            return storyboard;
        }

        /// <summary>
        /// Raises the power level captured event.
        /// </summary>
        private void RaisePowerLevelCapturedEvent()
        {
            // Raise the event by using the () operator.
            if (PowerLevelCaptured != null)
            {
                PowerLevelCaptured(this, new PowerLevelEventArgs(PowerLevel));
            }
        }

        /// <summary>
        /// Handles the Click event of the startPowerButton control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
        private void startPowerButton_Click(object sender, RoutedEventArgs e)
        {
            RaisePowerLevelCapturedEvent();
            startPowerButton.IsEnabled = false;
        }

        #endregion Methods
    }
}